package com.dx.financehy.modules.security.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

/**
 * @author wangwu
 * @date 2020/10/15
 */
@Slf4j
public class WxPublic {

    public static String getOpenId(String requestStr, String appId, String secret) {
        String result = "";
        try {
            String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + appId + "&secret=" + secret + "&code=" + requestStr + "&grant_type=authorization_code";
            HttpClient client = new DefaultHttpClient();
            HttpGet httpGet = new HttpGet(url);
            HttpResponse response = client.execute(httpGet);
            result = EntityUtils.toString(response.getEntity(), "UTF-8");
        } catch (Exception e) {
            log.error("调用微信接口失败", e);
        }
        return result;
    }

    //获取授权用户信息
//    public static String RequestMethoPost2(String requestStr,String appId,String secret) throws Exception {
//        //appid =wx23c7bcb65e965412
//        //secret = 13e89033ac9a78569a7057e9703fbd7a(新冠原来的)
//        String urlPath = "https://api.weixin.qq.com/sns/oauth2/access_token?appid="+appId+"&secret="+secret+"&code="+requestStr+"&grant_type=authorization_code";
//        StringBuffer sbf = new StringBuffer();
//        BufferedWriter writer = null;
//        BufferedReader reader = null;
//        HttpURLConnection uc = null;
//        try {
//            URL url = new URL(urlPath);
//            uc = (HttpURLConnection)url.openConnection();
//            uc.setDoOutput(true);
//            uc.connect();
//        /*
//        writer = new BufferedWriter(
//                new OutputStreamWriter(uc.getOutputStream())); //向服务器传送数据
//
//        // 使用URLEncoder.encoder(s,enc)进行编码
//
//        writer.flush();
//        writer.close(); // 关闭输出流
//        */
//
//            // 提交POST请求
//            reader = new BufferedReader(
//                    new InputStreamReader(uc.getInputStream(), "utf-8"));//读取服务器响应信息
//            String line;
//            while ((line = reader.readLine()) != null){
//                sbf.append(line);
//            }
//            reader.close();
//            uc.disconnect();
//        } catch (IOException e) {
////            log.error("调用微信接口失败",e);
//        } finally{
//            closeIO(writer,reader); //关闭流
//        }
//        return  sbf.toString().trim();
//    }
//
//    private  static void closeIO(BufferedWriter writer, BufferedReader reader){
//        if (writer != null) {
//            try {
//                writer.close();
//                writer = null;
//            } catch (Exception e) {
//
//            }
//        }
//        if (reader != null) {
//            try {
//                reader.close();
//                reader = null;
//            } catch (Exception e) {}
//        }
//    }
}
